/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package rigo.ricard.pris.rs.ac.uns.dmi.service.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import rigo.ricard.pris.rs.ac.uns.dmi.model.Advertising;
import rigo.ricard.pris.rs.ac.uns.dmi.service.AdvertisingLocalServiceUtil;
import rigo.ricard.pris.rs.ac.uns.dmi.service.base.AdvertisingLocalServiceBaseImpl;

import ai.aitia.liferay.servicebuilder.PersistenceUtils;

import com.liferay.portal.kernel.dao.orm.Criterion;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
import com.liferay.portal.kernel.exception.SystemException;

/**
 * The implementation of the advertising local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link rigo.ricard.pris.rs.ac.uns.dmi.service.AdvertisingLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author Rigo Richard
 * @see rigo.ricard.pris.rs.ac.uns.dmi.service.base.AdvertisingLocalServiceBaseImpl
 * @see rigo.ricard.pris.rs.ac.uns.dmi.service.AdvertisingLocalServiceUtil
 */
public class AdvertisingLocalServiceImpl extends AdvertisingLocalServiceBaseImpl {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never reference this interface directly. Always use {@link rigo.ricard.pris.rs.ac.uns.dmi.service.AdvertisingLocalServiceUtil} to access the advertising local service.
	 */
	
	public List<Advertising> getAdverisignsByUserId(long userId) throws SystemException {
		return advertisingPersistence.findByuserId(userId);
	}
	
	public List<Advertising> getTopAdverisigns() throws SystemException {
		List<Advertising> res = new ArrayList<Advertising>();
		String sql = "SELECT carModelId, (select count(*) from car_Advertising c1 where c1.carModelId = c.carModelId) " +
					 "num_of_ad FROM car_Advertising c group by carModelId order by num_of_ad desc;";
		List<Object[]> objects = PersistenceUtils.nativeQueryForList(advertisingPersistence, sql,  Object[].class);
		for(Object[] o : objects) {
			Long id = ((BigInteger) o[0]).longValue();
			res.addAll(advertisingPersistence.findByCarModelId(id));
		}
		return res;
	}

	public List<Advertising> getHotAdverisigns() throws SystemException {
		DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Advertising.class);
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.HOUR_OF_DAY, -24);
		Date date = calendar.getTime();
		Criterion criterion = PropertyFactoryUtil.forName("createDate").ge(date);
		dynamicQuery.add(criterion);
		List<Advertising> res = null;
		res = AdvertisingLocalServiceUtil.dynamicQuery(dynamicQuery);
		List<Advertising> retList = new ArrayList<Advertising>();
		for(Advertising p : res) {
			if(p!=null) {
				retList.add(p);
			}
		}
		return retList;
	}
}